package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.myview.MyListView;
import longcaisuyun.longcai.com.navi.BaseActivity;
import longcaisuyun.longcai.com.net.PostDingDanDele;
import longcaisuyun.longcai.com.net.PostDingXiang;
import longcaisuyun.longcai.com.net.PostMain;
import wh.utils.StringUtils;


//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//订单详情进行中的

/************************************************************/

public class DingDanOneActivity extends Activity implements View.OnClickListener {

    private Dialog dialog;
    private Dialog dialog_phone;
    TextView t_history, t_phone, t_people, t_paytype, t_usetime, t_qi, t_zhong, t_car, t_reward, t_tip, t_account,t_people1,t_phone1;
    TextView t_cost;
    ImageView iv_phone,iv_phone1;
    Button bt_location;
    String orderid = "";
    String qijing = "";
    String qiwei = "";
    String zhongjing = "";
    String zhongwei = "";
    String type;
    String mobile;
    String telephone;//客服电话
    String checkinfo="";//去目的地
    private String mobile1;
    private MyListView lv_tu;
    //
    private TextView tv_saycontent;
    private TextView tv_kuan,tv_dan,tv_ban,tv_kuanjin;

    //评价
    private TextView tv_name,tv_time,tv_pingjia;
    private RatingBar rb_ratingBar;
    private LinearLayout ll_pingjia;
    private ImageView iv_touxiang;
    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.iconmain) // 设置图片下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.iconmain) // 设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.iconmain) // 设置图片加载或解码过程中发生错误显示的图片
            .cacheInMemory(false) // 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
            .displayer(new FadeInBitmapDisplayer(100)).build(); // 构建完成
    private ImageLoader myimageloader = ImageLoader.getInstance();
    private List<String> list=new ArrayList<>();
    private Handler handler;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ding_dan_one);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);

        orderid = getIntent().getStringExtra("orderid");
        type = getIntent().getStringExtra("type");
        checkinfo=getIntent().getStringExtra("checkinfo");
        if (StringUtils.isEmpty(checkinfo)){
            checkinfo="none";
        }
        HttppostTel();
        ViewInit();
        HttpPost();
    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //评价
        tv_name= (TextView) findViewById(R.id.tv_name);
        tv_time= (TextView) findViewById(R.id.tv_time);
        tv_pingjia= (TextView) findViewById(R.id.tv_pingjia);
        rb_ratingBar= (RatingBar) findViewById(R.id.rb_ratingBar);
        ll_pingjia= (LinearLayout) findViewById(R.id.ll_pingjia);

        bt_location = (Button) findViewById(R.id.bt_location);
        bt_location.setOnClickListener(this);
        iv_phone = (ImageView) findViewById(R.id.iv_phone);
        iv_phone.setOnClickListener(this);
        t_history = (TextView) findViewById(R.id.t_history);
        t_history.setOnClickListener(this);
        t_phone = (TextView) findViewById(R.id.t_phone);
        t_people = (TextView) findViewById(R.id.t_people);
        t_paytype = (TextView) findViewById(R.id.t_paytype);
        t_usetime = (TextView) findViewById(R.id.t_usetime);
        t_qi = (TextView) findViewById(R.id.t_qi);
        t_zhong = (TextView) findViewById(R.id.t_zhong);
        t_car = (TextView) findViewById(R.id.t_car);
        t_reward = (TextView) findViewById(R.id.t_reward);
        t_tip = (TextView) findViewById(R.id.t_tip);
        t_account = (TextView) findViewById(R.id.t_account);
        t_people1= (TextView) findViewById(R.id.t_people1);
        t_phone1= (TextView) findViewById(R.id.t_phone1);
        iv_phone1= (ImageView) findViewById(R.id.iv_phone1);
        iv_phone1.setOnClickListener(this);
        iv_touxiang= (ImageView) findViewById(R.id.iv_touxiang);
        tv_saycontent= (TextView) findViewById(R.id.tv_saycontent);
        t_cost = (TextView) findViewById(R.id.t_cost);
        t_cost.setOnClickListener(this);


        //备注
        tv_kuan= (TextView) findViewById(R.id.tv_kuan);
        tv_dan= (TextView) findViewById(R.id.tv_dan);
        tv_ban= (TextView) findViewById(R.id.tv_ban);
        tv_kuanjin= (TextView) findViewById(R.id.tv_kuanjin);

        lv_tu= (MyListView) findViewById(R.id.lv_tu);
        if (type.equals("2")) {
            t_history.setVisibility(View.GONE);
            bt_location.setVisibility(View.GONE);
        }
        if (checkinfo.equals("20")){
            ll_pingjia.setVisibility(View.VISIBLE);
            bt_location.setVisibility(View.GONE);
        }else{
            ll_pingjia.setVisibility(View.GONE);
            bt_location.setVisibility(View.VISIBLE);
        }


        if(checkinfo.equals("-10")){
            t_history.setVisibility(View.VISIBLE);
            t_history.setText("删除订单");
        }
        Log.e("type", type);
    }
    public void sethandler(Handler handler){
        this.handler=handler;
    }
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.t_history:
                if(checkinfo.equals("-10")){
                    diag();
                }else{
                    diag_exit();
                }
                break;
            case R.id.iv_phone:
                diag_phone(mobile);
                break;
            case R.id.iv_phone1:
                diag_phone(mobile1);
                break;
            case R.id.bt_location:
                Intent intent = new Intent();
                intent.setClass(DingDanOneActivity.this, BaseActivity.class);
                intent.putExtra("qijing", qijing);
                intent.putExtra("qiwei", qiwei);
                intent.putExtra("zhongjing", zhongjing);
                intent.putExtra("zhongwei", zhongwei);
                intent.putExtra("checkinfo",checkinfo);
                startActivity(intent);
                break;
            case R.id.t_cost:
                Intent intent2=new Intent(DingDanOneActivity.this,CostActivity.class);
                intent2.putExtra("orderid",orderid);
                startActivity(intent2);
                break;

        }
    }

    public void diag_exit() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_dingdan_quxiao,
                null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.show();
        dialog.setContentView(layout);
        TextView textView28= (TextView) layout.findViewById(R.id.textView28);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
            tv_phone.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    dialog.dismiss();
                    Intent intent=new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:"+telephone));
                    startActivity(intent);
                }
            });


        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    //删除订单接口
    public void diag() {
        LayoutInflater inflater = LayoutInflater.from(DingDanOneActivity.this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_caozuo_phone, null);
        TextView tv_title = (TextView) layout.findViewById(R.id.textView28);
        tv_title.setText("是否删除该条订单");
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(DingDanOneActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(layout);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        tv_phone.setText("删除");
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                MyApplication.myviewutil.startProgressDialog(DingDanOneActivity.this);
                new PostDingDanDele(MyApplication.myPreferences.readUid(),orderid , new AsyCallBack<PostDingDanDele.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, PostDingDanDele.Info info) throws Exception {
                        super.onSuccess(toast, type, info);
                        if (info.message.equals("0")) {
                            Toast.makeText(DingDanOneActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(DingDanOneActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);

                    }
                }).execute(DingDanOneActivity.this);
                dialog.dismiss();
                MyApplication.myviewutil.stopProgressDialog();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                dialog.dismiss();

            }
        });
    }

    public void diag_phone(final String mobile) {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_dingdan_phone,
                null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog_phone = new Dialog(this);
        dialog_phone.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog_phone.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog_phone.show();
        dialog_phone.setContentView(layout);
        TextView textView29 = (TextView) layout.findViewById(R.id.textView29);
        textView29.setText("客户手机号:" + mobile);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));
                DingDanOneActivity.this.startActivity(intent);
                //  dialog.dismiss();
                dialog_phone.dismiss();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                dialog_phone.dismiss();
            }
        });
    }

    public void HttpPost() {
        MyApplication.myviewutil.startProgressDialog(DingDanOneActivity.this);
        new PostDingXiang(MyApplication.myPreferences.readUid(), orderid, new AsyCallBack<PostDingXiang.Info>() {
            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

                MyApplication.myviewutil.stopProgressDialog();
            }

            @Override
            public void onSuccess(String toast, int type, PostDingXiang.Info info) throws Exception {
                super.onSuccess(toast, type, info);

                if (info.message.equals("0")) {
                    Toast.makeText(DingDanOneActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                } else {
                    t_people.setText("发货人：" + info.name);
                    t_phone.setText("联系电话：" + info.mobile);
                    mobile = info.mobile;
                    t_paytype.setText(info.paytype);
                    t_usetime.setText("服务时间：" + info.usetime);
                    t_car.setText("车型：" + info.car);
                    t_account.setText(info.account+"元+"+info.tip+"元");
                    t_people1.setText("收货人：" + info.name2);
                    t_phone1.setText("联系电话：" + info.mobile2);
                    mobile1=info.mobile2;
                    if (checkinfo.equals("20")){
                        tv_name.setText(info.username);
                        tv_time.setText(info.posttime);
                        tv_pingjia.setText(info.content);
                        rb_ratingBar.setRating(Float.parseFloat(info.star));
                        myimageloader.displayImage(publicUrl.headurl + info.avatar,
                                iv_touxiang, options);
                        Log.d("======================",publicUrl.headurl);


                    }
                    if (info.addresslist.length()>2){
                        for (int i = 1; i < info.addresslist.length()-1; i++) {
                            list.add(info.addresslist.optJSONObject(i).optString("address"));
                        }
                        TuJingAdapter adapter=new TuJingAdapter(DingDanOneActivity.this,list);
                        lv_tu.setAdapter(adapter);
                    }
                    if (info.ismove.equals("0")){
                        tv_ban.setVisibility(View.GONE);
                    }else{
                        tv_ban.setVisibility(View.VISIBLE);
                    }
                    if (info.isbackorder.equals("0")){
                        tv_dan.setVisibility(View.GONE);
                    }else{
                        tv_dan.setVisibility(View.VISIBLE);
                    }
                    if (info.isbackmoney.equals("0")){
                        tv_kuan.setVisibility(View.GONE);
                        tv_kuanjin.setVisibility(View.GONE);
                    }else{
                        tv_kuan.setVisibility(View.VISIBLE);
                        tv_kuanjin.setVisibility(View.VISIBLE);
                        tv_kuanjin.setText("带回款金额为" + info.backmoney + "元");
                    }
                    if (info.ismove.equals("0")&&info.isbackorder.equals("0")&&info.isbackmoney.equals("0")){
                        tv_kuanjin.setVisibility(View.VISIBLE);
                        tv_kuanjin.setText("无");
                    }

                    tv_saycontent.setText("备注:"+info.saycontent);
                   /* t_tip.setText(info.tip);
                    t_reward.setText( info.reward);*/
                    t_qi.setText(info.addresslist.optJSONObject(0).optString("address"));
                    t_zhong.setText(info.addresslist.optJSONObject(info.addresslist.length() - 1).optString("address"));
                    String[] loc1 = info.addresslist.optJSONObject(0).optString("point").split(",");
                    String[] loc2 = info.addresslist.optJSONObject(info.addresslist.length() - 1).optString("point").split(",");
                    qijing = loc1[0];
                    qiwei = loc1[1];
                    zhongjing = loc2[0];
                    zhongwei = loc2[1];
                }
                MyApplication.myviewutil.stopProgressDialog();
            }
        }).execute(DingDanOneActivity.this);

    }
    private void HttppostTel() {
        MyApplication.myviewutil.startProgressDialog(DingDanOneActivity.this);

        new PostMain(MyApplication.myPreferences.readUid(), new AsyCallBack<PostMain.Info>() {

            @Override
            public void onSuccess(String toast, int type, PostMain.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("0")) {
                    Toast.makeText(DingDanOneActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                    MyApplication.myviewutil.stopProgressDialog();
                } else {
                    telephone = info.telephone;
                    MyApplication.myviewutil.stopProgressDialog();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                Toast.makeText(DingDanOneActivity.this, "获取数据失败,请检查您的网络设置后，再重新尝试", Toast.LENGTH_SHORT).show();
                MyApplication.myviewutil.stopProgressDialog();
            }
        }).execute(DingDanOneActivity.this);
    }

}
